//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YoYInflationBlackCapFloorEngine.h"
using namespace Cephei::QL::Pricingengines::Inflation;
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
#include <gen/QL/Pricingengines/Inflation/YoYInflationCapFloorEngine.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (Cephei::QL::Indexes::IYoYInflationIndex^ index, Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ volatility) : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
    CYoYInflationIndex^ _Cindex;
    CYoYOptionletVolatilitySurface^ _Cvolatility;
    try
    {
#ifdef HANDLE
        _phYoYInflationBlackCapFloorEngine = NULL;
#endif
        _Cindex = safe_cast<CYoYInflationIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::YoYInflationIndex>& _index = static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>&> (_Cindex->GetShared ()); 
        _Cvolatility = safe_cast<CYoYOptionletVolatilitySurface^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::YoYOptionletVolatilitySurface>& _volatility = static_cast<Handle<QuantLib::YoYOptionletVolatilitySurface>&> (_Cvolatility->GetHandle ()); 
        _ppYoYInflationBlackCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine> (new QuantLib::YoYInflationBlackCapFloorEngine ( _index,  _volatility ));
        SetYoYInflationCapFloorEngine (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
    }
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine>& childNative, Object^ owner) : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phYoYInflationBlackCapFloorEngine = NULL;
#endif
	_ppYoYInflationBlackCapFloorEngine = &childNative;
    _ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (QuantLib::YoYInflationBlackCapFloorEngine& childNative, Object^ owner) : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phYoYInflationBlackCapFloorEngine = NULL;
#endif
	_ppYoYInflationBlackCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine> (&childNative);
    _ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
    _YoYInflationBlackCapFloorEngineOwner = owner;
    _YoYInflationCapFloorEngineOwner = owner;
}

Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (CYoYInflationBlackCapFloorEngine^ copy) : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phYoYInflationBlackCapFloorEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationBlackCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine> (copy->GetShared());
        _ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
    }
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (PLATFORM::Type^ t) : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phYoYInflationBlackCapFloorEngine = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationBlackCapFloorEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (QuantLib::Handle<QuantLib::YoYInflationBlackCapFloorEngine>& childNative, Object^ owner)  : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
	_phYoYInflationBlackCapFloorEngine = &childNative;
	_ppYoYInflationBlackCapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine>>(childNative.currentLink());
    _ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
    _YoYInflationBlackCapFloorEngineOwner = owner;
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (QuantLib::Handle<QuantLib::YoYInflationBlackCapFloorEngine> childNative)  : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
	_phYoYInflationBlackCapFloorEngine = &childNative;
	_ppYoYInflationBlackCapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine>>(childNative.currentLink());
    _ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::CYoYInflationBlackCapFloorEngine (QuantLib::YoYInflationBlackCapFloorEngine childNative)  : CYoYInflationCapFloorEngine(CYoYInflationBlackCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phYoYInflationBlackCapFloorEngine = NULL;
#endif
	_ppYoYInflationBlackCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine> (new QuantLib::YoYInflationBlackCapFloorEngine (childNative));
    _ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine));
}
#endif

Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::~CYoYInflationBlackCapFloorEngine ()
{
    if (_ppYoYInflationBlackCapFloorEngine != NULL)
    {
	    delete _ppYoYInflationBlackCapFloorEngine;
        _ppYoYInflationBlackCapFloorEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::!CYoYInflationBlackCapFloorEngine ()
{
    if (_ppYoYInflationBlackCapFloorEngine != NULL)
    {
	    delete _ppYoYInflationBlackCapFloorEngine;
    }
}
QuantLib::YoYInflationBlackCapFloorEngine& Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::GetReference ()
{
    if (_ppYoYInflationBlackCapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppYoYInflationBlackCapFloorEngine;
}
boost::shared_ptr<QuantLib::YoYInflationBlackCapFloorEngine>& Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::GetShared ()
{
    if (_ppYoYInflationBlackCapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppYoYInflationBlackCapFloorEngine;
}
QuantLib::YoYInflationBlackCapFloorEngine* Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::GetPointer ()
{
    if (_ppYoYInflationBlackCapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppYoYInflationBlackCapFloorEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationBlackCapFloorEngine>& Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::GetHandle ()
{
	if (_phYoYInflationBlackCapFloorEngine == NULL)
	{
		_phYoYInflationBlackCapFloorEngine = new Handle<QuantLib::YoYInflationBlackCapFloorEngine> (*_ppYoYInflationBlackCapFloorEngine);
	}
	return *_phYoYInflationBlackCapFloorEngine;
}
#endif
bool Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine::HasNative () 
{
	return (_ppYoYInflationBlackCapFloorEngine != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Inflation::IYoYInflationBlackCapFloorEngine^ Cephei::QL::Pricingengines::Inflation::CYoYInflationBlackCapFloorEngine_Factory::Create (Cephei::QL::Indexes::IYoYInflationIndex^ index, Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ volatility)
{
    return REFNEW CYoYInflationBlackCapFloorEngine ( index,  volatility);
}
